package nc.impl.ic.arrive.other;

import java.util.List;
import java.util.Map;

import com.informix.msg.isam_en_US;

import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.dao.BaseDAO;
import nc.cmbd.itf.scmpub.reference.uap.pf.PfServiceScmUtil;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.am.common.util.ArrayConstructor;
import nc.vo.ic.m4a.entity.GeneralInVO;
import nc.vo.pu.m23.entity.ArriveVO;
import nc.vo.pub.BusinessException;

public class PushToRuku1 implements IBusinessListener {

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		BusinessEvent businessEvent = (BusinessEvent) event;
		Object userObj = businessEvent.getObject();
		BaseDAO dao =new BaseDAO();
		int ext = 1;
		ArriveVO[] arriveVOs1 = (ArriveVO[])ArrayConstructor.getArray(userObj);
		ArriveVO[] arriveVOs = arriveVOs1.clone();
        for(int i=0;i<arriveVOs.length;i++){
        	arriveVOs[i].getHVO().getVbillcode();
        	for(int j=0;j<arriveVOs[i].getBVO().length;j++){
        	
        	StringBuffer kczz = new StringBuffer();
        	kczz.append("  select  pk_receivestore,bd_stordoc.DEF3,org_orgs.pk_vid");
        	kczz.append(" from po_arriveorder");
        	kczz.append(" left join po_arriveorder_b");
        	kczz.append(" on po_arriveorder.pk_arriveorder = po_arriveorder_b.pk_arriveorder");
        	kczz.append(" left join bd_stordoc");
        	kczz.append(" on po_arriveorder_b.pk_receivestore  = bd_stordoc.pk_stordoc");
           	kczz.append(" left join org_orgs");
        	kczz.append(" on bd_stordoc.DEF3 = org_orgs.pk_org");  	
        	kczz.append(" where po_arriveorder.vbillcode ='"+arriveVOs[i].getHVO().getVbillcode()+"'");
    		List<Map<String, Object>> list =  (List<Map<String, Object>>) dao.executeQuery(kczz.toString(), new MapListProcessor());
    		for(int k=0;k<list.size();k++){
    			Object mahkczz = list.get(k).get("def3");
    			if(mahkczz==null||mahkczz.equals(""))
    			{
    				ext++;
    			}
    			else{
    				arriveVOs[i].getHVO().setPk_org(list.get(k).get("def3").toString());
    				arriveVOs[i].getHVO().setPk_org_v(list.get(k).get("pk_vid").toString());
    				arriveVOs[i].getBVO()[j].setPk_org(list.get(k).get("def3").toString());
    				arriveVOs[i].getBVO()[j].setPk_org_v(list.get(k).get("pk_vid").toString());
    				String ckzzdzb ="select ck from ckzzdzb where zz = '"+list.get(k).get("def3").toString()+"'";
    	    		List<Map<String, Object>> cklist =  (List<Map<String, Object>>) dao.executeQuery(ckzzdzb, new MapListProcessor());
    	    		if(cklist.size()>0){
    	    		arriveVOs[i].getBVO()[j].setPk_receivestore(cklist.get(0).get("ck").toString());
    	    		}
    			}
    		}
          }
        }
        if(ext==1){
    		GeneralInVO[] retAggVos = PfServiceScmUtil.exeVOChangeByBillItfDef("23", "4A", arriveVOs);
//    		nc.itf.uap.pf.IplatFormEntry iIplatFormEntry = NCLocator.getInst0ance().lookup(nc.itf.uap.pf.IplatFormEntry.class);	
    		nc.itf.ic.m4a.IGeneralInMaintain service = nc.bs.framework.common.NCLocator.getInstance().lookup(nc.itf.ic.m4a.IGeneralInMaintain.class);
    	    service.insert(retAggVos);
        }

	}
	


}
